// pages/pro_info/priInfo.js
const app = getApp()

Page({
  data: {
    navbar: ['商品', '详情', '评价'],
    currentTab: 0,
    obj: [],
    baseUrl: app.d.ceshiUrl,
    bannerItem: [],
    goodsId: 0,
    Price: 0,
    count: 1,
  },

  bindMinus: function (e) {
    var that = this;
    var cout = parseInt(that.data.count);
    if (that.data.count > 1) {
      that.setData({
        count: cout - 1,      
      });
    }
  },
  bindManual: function (e) {
    console.log(e.detail.value)
    var count = e.detail.value;
    var that = this;  
      that.setData({
        count: count,
      });  
  },
  bindPlus: function (e) {
    var that = this;
    var cout = parseInt(that.data.count);
    console.log('------e-------');
    that.setData({
      count: cout + 1
    });
  },
  navbarTap: function (e) {
    this.setData({ currentTab: e.currentTarget.dataset.idx })
  },

  addShopCart: function (e) { //添加到购物车
    var that = this;

    console.log("addShopCart");
    // app.seclectContract(1);
    that.addToCat();
  },

  addToCat: function () {
    var that = this;
    wx.request({
      url: app.d.ceshiUrl + '/transaction/StoreCartMgrAction!addGoods',
      method: 'get',
      data: {
        "goodsCart.goods.id": that.data.goodsId,
        'goodsCart.count': that.data.count,
        'goodsCart.price': that.data.Price,
      },
      header: app.globalData.header,
      success: function (res) {
        var data = res.data;
        console.log(res)
        console.log(res.data.msg);
        if (data.success == 'false') {
          wx.showToast({
            title: '请重新登录',
            duration: 2000
          });
        } else {
          wx.showToast({
            title: '加入购物车成功',
            icon: 'success',
            duration: 2000
          });
        }        
      },
      fail: function () {
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      }
    });
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var id = options.id;
    console.log(id)
    wx.request({
      url: app.d.ceshiUrl + '/goods/GoodsMgrAction!loadById?goodsId=' + id,
      method: 'post',
      header: app.globalData.header,
      success: function (res) {
        console.log(res)
        var obj1 = res.data.obj;
        
        that.setData({
          obj: obj1,
          goodsId: id,
          Price: obj1.goods_price,
          bannerItem: obj1.goods_main_photo_path,
        });

      },
      fail: function (e) {
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      },
    })
  },
  reback: function () {
    wx.navigateBack({
      delta: 1
    })
  },

  gotocart: function () {
    wx.switchTab({
      url: '../cart/cart',
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})